//
//  LGHelpServiceController.m
//  RedSunDigitalDoctor
//
//  Created by 1234 on 2022/9/28.
//

#import "LGHelpServiceController.h"
#import "LGHelpQuestionController.h"
//打电话
#import "LGHelpServicePhoneView.h"
#import "LGNavigationView.h"
#define topHeight  (topBarHeight+viewPix(235))
@interface LGHelpServiceController ()<LGEmptyViewDelegate>
/**   */
@property (nonatomic , strong)NSMutableArray *controllerArray;
/**   */
@property (nonatomic , strong)LGNavigationView *navigationView;
/**   */
@property (nonatomic , strong)UIImageView *baseImageView;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIButton *connectBtn;
/**   */
@property (nonatomic , strong)LGEmptyView *emptyView;
/**   */
@property (nonatomic , strong)NSArray *titleArray;
/**   */
@property (nonatomic , strong)NSArray *widthArray;
/**   */
@property (nonatomic , copy)NSString *phoneNumber;
/**   */
@property (nonatomic , copy)NSString *serviceTime;
/**   */
@property (nonatomic , assign)BOOL isFirst;
@end

@implementation LGHelpServiceController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self commonInit];
    [self createTopView];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, topHeight, Screen_W, Screen_H-topHeight)];
    [self requestData];
}
-(void)backButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)startBufferAction{
    if (self.isFirst == YES) {
        [self.emptyView startBufferAction];
        self.isFirst = NO;
    }
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/help/categorylists" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isFirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            NSArray *tempArray = returnData;
            NSMutableArray *titleArray = [NSMutableArray array];
            NSMutableArray *widthArray = [NSMutableArray array];
            for (NSInteger i=0; i<tempArray.count; i++) {
                NSDictionary *tempDic = tempArray[i];
                NSString *title = LGNSString(tempDic[@"title"]);
                [titleArray addObject:title];
                LGHelpQuestionController *controller = [[LGHelpQuestionController alloc]init];
                controller.catogryId = LGNSString(tempDic[@"category_id"]);
                [self.controllerArray addObject:controller];
                [self addChildViewController:controller];
                CGFloat width = [title textWidthWithFont:LGFontWeight(15, UIFontWeightMedium) maxHeight:MAXFLOAT];
                [widthArray addObject:@(width+35)];
            }
            self.titleArray = titleArray;
            self.widthArray = widthArray;
            self.progressViewWidths = self.widthArray;
            [self reloadData];
        }
        if(self.titleArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isFirst = NO;
        [self.emptyView stopBufferAction];
        if(self.titleArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:0];
        }
    }];
    
    [RequestUtil withPOST:@"/common/dict/kefu" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.phoneNumber = LGNSString(returnData[@"phone"]);
            self.serviceTime = [NSString stringWithFormat:@"%@~%@",returnData[@"service_open_time"],returnData[@"service_close_time"]];
            self.timeLabel.text = [NSString stringWithFormat:@"工作时间 %@~%@",returnData[@"service_open_time"],returnData[@"service_close_time"]];
        }
    } error:^(NSError *error) {
        
    }];
}

//联系客服
-(void)phoneBtnAction{
    LGHelpServicePhoneView *phoneView = [[LGHelpServicePhoneView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    phoneView.seviceTime = self.serviceTime;
    [self.view.window addSubview:phoneView];
    phoneView.makePhoneCallAction = ^{
        NSString *callNumber = [NSString stringWithFormat:@"telprompt://%@",self.phoneNumber];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callNumber] options:@{} completionHandler:^(BOOL success) {
        }];
    };
}


#pragma mark
#pragma mark ====> WMPageControllerDelegate
- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return self.titleArray.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    return self.titleArray[index];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    return self.controllerArray[index];
}

- (CGFloat)menuView:(WMMenuView *)menu widthForItemAtIndex:(NSInteger)index {
    return [self.widthArray[index] floatValue];
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    return CGRectMake(0, topHeight, Screen_W, viewPix(30));
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    return CGRectMake(0, topHeight+viewPix(30), Screen_W, Screen_H-topHeight-viewPix(30));
}

- (void)pageController:(WMPageController *)pageController willEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    NSLog(@">>>>>willEnterView>>>%@",viewController.title);
}


- (void)pageController:(WMPageController *)pageController didEnterViewController:(__kindof UIViewController *)viewController withInfo:(NSDictionary *)info{
    NSLog(@">>>>>didEnterView>>>%@",viewController.title);
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark
#pragma mark ====> 创建控件
-(void)commonInit{
    self.title = @"帮助与客服";
    self.titleColorNormal = [UIColor colorWithString:@"#666666"];
    self.titleColorSelected = [UIColor colorWithString:@"#2E9AFC"];
    self.progressColor = [UIColor colorWithString:@"#EAF4FF"];
    self.titleSizeSelected = 14;
    self.titleSizeNormal = 12;
    
    self.titleFontName = @"STHeitiTC-Semibold";
    self.menuViewStyle = WMMenuViewStyleFlood;
    self.menuViewLayoutMode = WMMenuViewLayoutModeLeft;
    self.menuViewContentMargin = 10;
    self.menuView.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(15), viewPix(30))];
//    self.progressWidth = (Screen_W-viewPix(20))/4.0;
    self.progressHeight = viewPix(30);
    self.progressViewCornerRadius = viewPix(15);
    self.progressViewBottomSpace = 4;
    self.menuViewContentMargin = 16;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.connectBtn];
}

-(NSMutableArray *)controllerArray{
    if(!_controllerArray){
        _controllerArray = [NSMutableArray array];
    }
    return _controllerArray;
}

-(UIButton *)connectBtn{
    if(!_connectBtn){
        _connectBtn = [UIButton buttonWithTitle:@"立即拨打" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _connectBtn.backgroundColor = [UIColor colorWithString:@"#429BFF"];
        _connectBtn.cornerRidus = viewPix(18);
        [_connectBtn addTarget:self action:@selector(phoneBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _connectBtn;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"工作时间：" colorString:@"#999999" textFont:LGFont(13) textAlignment:NSTextAlignmentCenter lines:1];
    }
    return _timeLabel;
}

-(void)createTopView{
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView *topView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -viewPix(20), Screen_W, viewPix(215))];
    topView.image = [UIImage imageNamed:@"客服帮助-背景"];
    [self.view addSubview:topView];
    
    LGNavigationView *navigationView = [[LGNavigationView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, topBarHeight)];
    navigationView.backgroundColor = [UIColor clearColor];
    navigationView.type = LGNavigationTypeWhite;
    navigationView.title = @"客服与帮助";
    [navigationView.backBtn addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:navigationView];
    
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), viewPix(150))];
    [baseView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
    baseView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:baseView];
    
    UIImageView *iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"客服帮助-电话"]];
    iconView.frame = CGRectMake(viewPix(28), viewPix(25), viewPix(100), viewPix(100));
    [baseView addSubview:iconView];
    
    UILabel *titleLabel = [UILabel lableWithText:@"电话客服" colorString:@"#333333" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
    [baseView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right);
        make.right.equalTo(baseView);
        make.top.equalTo(baseView).offset(viewPix(33));
    }];
    
    [baseView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(viewPix(5));
        make.left.right.equalTo(titleLabel);
    }];
    [baseView addSubview:self.connectBtn];
    [self.connectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timeLabel.mas_bottom).offset(viewPix(15));
        make.centerX.equalTo(self.timeLabel);
        make.width.equalTo(@(viewPix(137)));
        make.height.equalTo(@(viewPix(36)));
    }];
    UILabel *questionLabel = [UILabel lableWithText:@"常见问题" colorString:@"#333333" textFont:LGFontWeight(20, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    questionLabel.frame = CGRectMake(viewPix(23), kMaxY(baseView.frame)+viewPix(23), viewPix(120), 25);
    [self.view addSubview:questionLabel];
}


-(void)emptyViewWithSuperView:(UIView *)superView frame:(CGRect)frame{
    LGEmptyView *emptyView = [[LGEmptyView alloc]initWithFrame:frame];
    emptyView.backgroundColor = [UIColor whiteColor];
    emptyView.margin = viewPix(15);
    emptyView.delegate = self;
    emptyView.needBuffer = YES;
    [superView addSubview:emptyView];
    self.emptyView = emptyView;
    [self performSelector:@selector(startBufferAction) withObject:nil afterDelay:0.5];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
